package com.leapp.sk.viewdelegate.im

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.imgselector.ImageSelector
import com.example.mvp.view.video.VideoActivity
import com.example.util.BCFinalist
import com.leapp.sk.R
import com.leapp.sk.bean.EmojiconEntity
import com.example.im.SKIMSend
import com.leapp.sk.ui.activity.classs.ClassDetialActivity
import com.example.util.im.MessagePresenter
import com.example.util.finallist.MESSAGE_LIST_ID
import com.leapp.sk.view.im.SKChatInputMenu
import com.leapp.sk.view.im.SKChatMessageList
import java.io.File

/**
 * 群组视图托管类
 * @author  chaohaipeng  on 2018/7/25.
 */
class GroupChatDelegale : BaseAppDelegate(), SKChatInputMenu.ChatInputMenuListener {

    /**
     * 会话Id
     */
    var conversationId: String? = null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_single_chat
    }

    /**
     * 初始化页面布局
     */
    fun initView(id: String) {
        conversationId = id
        val sim_asc_input_menu = getView<SKChatInputMenu>(R.id.sim_asc_input_menu)
        sim_asc_input_menu.setChatInputMenuListener(this)
        sim_asc_input_menu.init(getActivity<AppCompatActivity>())
        initListData(id)
    }

    /**
     * 判断菜单是否打开
     */
    fun meunIsOpen(): Boolean {
        val sim_asc_input_menu = getView<SKChatInputMenu>(R.id.sim_asc_input_menu)
        if (sim_asc_input_menu.meunIsOpen()) {
            return true
        }
        return false
    }

    /**
     * 头像选择
     */
    private fun pickImage() {
        ImageSelector.getInstance()
                .setSelectModel(ImageSelector.MULTI_MODE)
                .setMaxCount(9)
                .setGridColumns(3)
                .setShowCamera(true)
                .startSelect(getActivity<AppCompatActivity>())
    }

    /**
     * 初始化页面数据
     */
    fun initListData(id: String) {
        val sml_asc_list = getView<SKChatMessageList>(R.id.sml_asc_list)
        sml_asc_list.init(getActivity(), id)
        sml_asc_list.loadMessage(id, "", 20)
    }


    /**
     * 设置标题
     */
    fun initTobBar(title: String?) {
        val topBar = getView<SKTopBar>(R.id.jtb_asc)
        topBar.setRightBtnImage(R.mipmap.icon_group_member_info)
        topBar.setRightBtnImageShow(true)
        if (!TextUtils.isEmpty(title)) {
            topBar?.setTitle(title)
        }
        topBar.setEventInterface(object : SKTopBar.EventInterface {
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()
            }

            override fun rightOnClick() {
                if (!TextUtils.isEmpty(conversationId)){
                    val intent: Intent = Intent(getActivity<AppCompatActivity>(), ClassDetialActivity::class.java)
                    intent.putExtra(MESSAGE_LIST_ID, conversationId)
                    getActivity<AppCompatActivity>().startActivity(intent)
                }
            }
        })

    }


    /**
     * 消息更新监听初始化
     */
    fun initMegListener(messagePresenter: MessagePresenter?) {
        if (messagePresenter != null) {
            val sml_asc_list = getView<SKChatMessageList>(R.id.sml_asc_list)
            sml_asc_list.initMegListener(messagePresenter)
        }
    }


    override fun onSendMessage(content: String) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendTxt(conversationId!!, content, LKIMChatType.GroupChat)
        }

    }

    override fun onBigExpressionClicked(emojicon: EmojiconEntity) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendTxt(conversationId!!, emojicon.identityCode + "", LKIMChatType.GroupChat)
        }
    }

    override fun onImageExpressionClicked() {
        pickImage()
    }

    override fun onVideoExpressionClicked() {
        val intent: Intent = Intent(getActivity<AppCompatActivity>(), VideoActivity::class.java)
        getActivity<AppCompatActivity>().startActivityForResult(intent, BCFinalist.VIDEO_PLAY_REQUEST)
    }

    override fun onSendVoice(voiceDuration: Int, voicePath: String, voiceFileName: String, voiceFile: File) {
        if (conversationId != null) {
            SKIMSend.getInstance().sendVoice(conversationId!!, voiceFile.absolutePath, voiceDuration, LKIMChatType.GroupChat)
        }
    }
}